package com.cn.mmd.controller;

import com.cn.mmd.model.*;
import com.cn.mmd.service.*;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 学生管理
 * Created by Administrator on 2016/7/29 0029.
 */

@Controller
@RequestMapping(value = "page/manageStu")
public class ManageStudentController {

    @Autowired
    private StudentAppService studentAppService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private LogManageService logManageService;

    @Autowired
    private StudentGroupingService studentGroupingService;

    @Autowired
    private ShiroUserRoleService shiroUserRoleService;

    /**
     *根据学生id 或 学生名字 或学生班级 或学生身份证 查询学生
     * @param request
     * @return
     */
    @RequestMapping(value = "/select")
    @ResponseBody synchronized  public  Object manageSelectStu(HttpServletRequest request){
        Map<String,Object> objectMap = new HashMap<String, Object>();
        List<Map> mapList = new ArrayList<Map>();
        Map<String,Object> map;
        String manageId = request.getParameter("manageId");
        Integer statrow = Integer.parseInt(request.getParameter("startrow"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
        String studentId = request.getParameter("exist");
        List<Student> studentList;
        Student student = new Student();
        student.setStartrow(statrow);
        student.setPageSize(pageSize);
        student.setStudentid(studentId);
        student.setStudentremarks("离开");
        studentList = studentService.selectByPaging(student);
        StudentApp studentApp;
        if (studentList!=null&&studentList.size()>0){
            for (int i = 0 ; i < studentList.size() ; i ++){
                map = new HashMap<String, Object>();
                student = studentList.get(i);
                map.put("studentId", student.getStudentid());
                studentApp = studentAppService.selectByPrimaryKey(student.getStudentid());
                map.put("studentName", student.getStudentname());
                map.put("studentMajor", student.getStudentmajor());
                map.put("studentIdCard", student.getStudentidcard());
                map.put("studentClasss", student.getStudentclasss());
                map.put("studentClass", student.getStudentclass());
                map.put("studentCollege", student.getStudentcollege());
                map.put("studentRemarks", student.getStudentremarks());
                map.put("studentQQMail", student.getStudentqqmail());
                map.put("studentSchool", student.getStudentschool());
                map.put("studentSex", studentApp.getStudentsex());
                map.put("studentHeadPortrait", studentApp.getStudentheadportrait());
                map.put("personalizedSignature", studentApp.getPersonalizedsignature());
                map.put("studentPhoneNumber", studentApp.getStudentphonenumber());
                map.put("myBirThd", studentApp.getMybirthd());
                map.put("myNickName", studentApp.getMynickname());
                map.put("commentNum", studentApp.getCommentnum());
                map.put("viewedNum", studentApp.getViewednum());
                mapList.add(map);
            }
            if (mapList!=null&&studentList.size()==mapList.size()){
                student = new Student();
                student.setStudentid(studentId);
                student.setStudentremarks("离开");
                int i = studentService.countLimitNum(student);
                objectMap.put("code",200);
                objectMap.put("message","成功");
                objectMap.put("data",mapList);
                objectMap.put("total",i);
            }else {
                objectMap.put("code",201);
                objectMap.put("message","失败");
            }
        }else {
            objectMap.put("code",202);
            objectMap.put("message","学生列表为空");
        }
        return objectMap;
    }

    /**
     * 添加一个学生
     * @param request
     * @return
     */
    @RequestMapping(value = "/addOneStu")
    @ResponseBody synchronized  public Object manageAddOneStu(HttpServletRequest request){
        Map<String,Object> objectMap = new HashMap<String, Object>();
        String manageId = request.getParameter("manageId");
        String studentId = request.getParameter("studentId");
        String studentName = request.getParameter("studentName");
        String studentSex = request.getParameter("studentSex");
        String studentMajor = request.getParameter("studentMajor");
        String studentIdCard = request.getParameter("studentIdCard");
        String studentPhoneNumber = request.getParameter("studentPhoneNumber");
        String studentClass = request.getParameter("studentClass");
        String studentClasss = request.getParameter("studentClasss");
        String studentSchool = request.getParameter("studentSchool");
        String studentQQMail = request.getParameter("studentQQMail");
        String studentRemarks = request.getParameter("studentRemarks");
        String studentCollege = request.getParameter("studentCollege");
        if (studentService.selectByPrimaryKey(studentId)==null) {
            String s;
            Student student = new Student();
            student.setStudentclass(studentClass);
            student.setStudentclasss(studentClasss);
            student.setStudentcollege(studentCollege);
            student.setStudentid(studentId);
            student.setStudentidcard(studentIdCard);
            student.setStudentmajor(studentMajor);
            student.setStudentname(studentName);
            student.setStudentphonenumber(studentPhoneNumber);
            student.setStudentqqmail(studentQQMail);
            student.setStudentremarks(studentRemarks);
            student.setStudentschool(studentSchool);
            student.setStudentsex(studentSex);
            StudentApp studentApp = new StudentApp();
            studentApp.setStudentid(studentId);
            studentApp.setStudentsex(studentSex);
            studentApp.setStudentname(studentName);
            if (student.getStudentid().length()>5){
                s = student.getStudentid().substring(student.getStudentid().length()-6,student.getStudentid().length());
                studentApp.setStudentpassword(DigestUtils.md5Hex(s));
            }else {
                s="123456";
                studentApp.setStudentpassword(DigestUtils.md5Hex(s));
            }
            studentApp.setStudentphonenumber(studentPhoneNumber);
            ShiroUserRole shiroUserRole = new ShiroUserRole();
            shiroUserRole.setUserId(studentId);
            shiroUserRole.setRoleId(1);
            if (studentService.insertSelective(student) > 0&& studentAppService.insertSelective(studentApp)>0
                    &&shiroUserRoleService.insertSelective(shiroUserRole)>0) {
                objectMap.put("code", 200);
                objectMap.put("message", "成功");
                Date date = new Date();
                SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm");
                String logTime=time.format(date);
                LogManage logManage = new LogManage();
                logManage.setLogtime(logTime);
                logManage.setManageid(manageId);
                logManage.setLogcontent("添加了一个学生信息:" + studentId);
                logManageService.insertSelective(logManage);
            } else {
                objectMap.put("code", 201);
                objectMap.put("message", "失败");
            }
        }else {
            objectMap.put("code", 202);
            objectMap.put("message", "当前学号已存在");
        }
        return objectMap;
    }

    /**
     * 修改一个学生信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/updOneStu")
    @ResponseBody public synchronized  Object manageUpdOneStu(HttpServletRequest request){
        Map<String,Object> objectMap = new HashMap<String, Object>();
        String manageId = request.getParameter("manageId");
        String studentId = request.getParameter("studentId");
        String studentName = request.getParameter("studentName");
        String studentSex = request.getParameter("studentSex");
        String studentMajor = request.getParameter("studentMajor");
        String studentIdCard = request.getParameter("studentIdCard");
        String studentPhoneNumber = request.getParameter("studentPhoneNumber");
        String studentClass = request.getParameter("studentClass");
        String studentClasss = request.getParameter("studentClasss");
        String studentSchool = request.getParameter("studentSchool");
        String studentQQMail = request.getParameter("studentQQMail");
        String studentRemarks = request.getParameter("studentRemarks");
        String studentCollege = request.getParameter("studentCollege");
        Student student = new Student();
        student.setStudentclass(studentClass);
        student.setStudentclasss(studentClasss);
        student.setStudentcollege(studentCollege);
        student.setStudentid(studentId);
        student.setStudentidcard(studentIdCard);
        student.setStudentmajor(studentMajor);
        student.setStudentname(studentName);
        student.setStudentphonenumber(studentPhoneNumber);
        student.setStudentqqmail(studentQQMail);
        student.setStudentremarks(studentRemarks);
        student.setStudentschool(studentSchool);
        student.setStudentsex(studentSex);
        if (studentService.updateByPrimaryKeySelective(student)>0){
            String s;
            StudentApp studentApp = new StudentApp();
            studentApp.setStudentid(studentId);
            studentApp.setStudentsex(studentSex);
            studentApp.setStudentname(studentName);
            studentApp.setStudentphonenumber(studentPhoneNumber);
            if (studentAppService.updateByPrimaryKeySelective(studentApp)>0){
                objectMap.put("code", 200);
                objectMap.put("message", "成功");
                Date date = new Date();
                SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm");
                String logTime=time.format(date);
                LogManage logManage = new LogManage();
                logManage.setLogtime(logTime);
                logManage.setManageid(manageId);
                logManage.setLogcontent("修改了一个学生信息:"+studentId);
                logManageService.insertSelective(logManage);
            }else {
                objectMap.put("code", 201);
                objectMap.put("message", "失败");
            }
        }
        return objectMap;
    }

    /**
     * 删除一个学生。将一个学生班级设置为空 并填写备注
     * @param request
     * @return
     */
    @RequestMapping(value = "/delOneStu")
    @ResponseBody public synchronized  Object manageDelOneStu(HttpServletRequest request){
        Map<String,Object> objectMap = new HashMap<String, Object>();
        String manageId = request.getParameter("manageId");
        String studentId = request.getParameter("studentId");
        Student student = new Student();
        student.setStudentid(studentId);
        student.setStudentclass("");
        student.setStudentremarks("离开");
        StudentGrouping studentGrouping = new StudentGrouping();
        studentGrouping.setStuid(studentId);
        if (studentService.updateByPrimaryKeySelective(student)>0){
            studentGroupingService.deleteByStuId(studentGrouping);
            shiroUserRoleService.deleteByUserId(studentId);
               objectMap.put("code", 200);
               objectMap.put("message", "成功");
            Date date = new Date();
            SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String logTime=time.format(date);
            LogManage logManage = new LogManage();
            logManage.setLogtime(logTime);
            logManage.setManageid(manageId);
            logManage.setLogcontent("删除了一个学生信息:"+studentId);
            logManageService.insertSelective(logManage);
           }else {
               objectMap.put("code", 201);
               objectMap.put("message", "失败");
           }

        return objectMap;
    }

    /**
     * 批量删除学生 并 将学生设为 不可查询状态(开除)
     * @param request
     * @return
     */
    @RequestMapping(value = "/delStu")
    @ResponseBody public synchronized  Object manageDelStu(HttpServletRequest request){
        Map<String,Object> objectMap = new HashMap<String, Object>();
        List<Map> mapList = new ArrayList<Map>();
        Map<String,Object> map;
        String manageId = request.getParameter("manageId");
        String [] studentIds = request.getParameterValues("studentId");
        String studentId;
        for (int i = 0 ; i < studentIds.length ; i ++) {
            map = new HashMap<String, Object>();
            studentId = studentIds[i];
            Student student = new Student();
            student.setStudentid(studentId);
            student.setStudentclass("");
            student.setStudentremarks("开除");
            StudentGrouping studentGrouping = new StudentGrouping();
            studentGrouping.setStuid(studentId);
            if (studentService.updateByPrimaryKeySelective(student) > 0) {
                studentGroupingService.deleteByStuId(studentGrouping);
                shiroUserRoleService.deleteByUserId(studentId);
                map.put("studentId",studentId);
                map.put("code", 200);
                map.put("message", "成功");
                Date date = new Date();
                SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm");
                String logTime=time.format(date);
                LogManage logManage = new LogManage();
                logManage.setLogtime(logTime);
                logManage.setManageid(manageId);
                logManage.setLogcontent("删除了一个学生信息:"+studentId);
                logManageService.insertSelective(logManage);
            } else {
                map.put("studentId",studentId);
                map.put("code", 201);
                map.put("message", "有错误项");
            }
            mapList.add(map);
        }
        if (mapList!=null&&mapList.size()==studentIds.length){
            objectMap.put("data",mapList);
        }else {
            objectMap.put("code", 202);
            objectMap.put("message", "失败");
        }
        return objectMap;
    }
}
